package com.jdls.tanhua.dubbo.impl;

import cn.hutool.core.collection.CollUtil;
import com.jdls.tanhua.dubbo.UserLikeApi;
import com.jdls.tanhua.mongo.Friend;
import com.jdls.tanhua.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 嘉德罗斯
 * @date 2021/11/1
 */
@DubboService
public class UserLikeImpl implements UserLikeApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Map<String, Integer> findCounts(Long userId) {
        Map<String,Integer> map = new HashMap<>();
        //查询我喜欢的数量
        Criteria criteria= Criteria.where("userId").is(userId).and("isLike").is(true);
        Query query1 = Query.query(criteria);
        List<UserLike> userLikes = mongoTemplate.find(query1, UserLike.class);
        //设置我喜欢数量
        map.put("loveCount",userLikes.size());

        //查询我粉丝的数量
        Query query2 = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true));
        List<UserLike> userFans = mongoTemplate.find(query2, UserLike.class);
        //设置粉丝数量
        map.put("fanCount",userFans.size());

        //查询互相喜欢的数量
        List<Long> likeUserId = CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);
        Query query3 = Query.query(Criteria.where("userId").in(likeUserId).and("isLike").is(true));
        List<UserLike> likes = mongoTemplate.find(query3, UserLike.class);
        map.put("eachLoveCount",likes.size());


        return map;
    }

    @Override
    public boolean save(UserLike userLike) {
        boolean flag = false;
        if(userLike.getIsLike()){
            Criteria criteria = Criteria.where("likeUserId").is(userLike.getUserId()).and("userId").is(userLike.getLikeUserId())
                    .and("isLike").is(true);
            Query query = Query.query(criteria);
            UserLike like = mongoTemplate.findOne(query, UserLike.class);
            if(like!=null){
                try {
                    Query qu = Query.query(Criteria.where("userId").is(userLike.getUserId()).and("friendId").is(userLike.getLikeUserId()));
                    Friend friend = mongoTemplate.findOne(qu, Friend.class);
                    if (friend == null){
                        friend = new Friend();
                        friend.setCreated(System.currentTimeMillis());
                        friend.setFriendId(userLike.getUserId());
                        friend.setUserId(userLike.getLikeUserId());
                        mongoTemplate.save(friend);
                    }

                    Query q = Query.query(Criteria.where("userId").is(userLike.getUserId()).and("friendId").is(userLike.getLikeUserId()));
                    friend = mongoTemplate.findOne(q, Friend.class);
                    if (friend == null){
                        friend = new Friend();
                        friend.setCreated(System.currentTimeMillis());
                        friend.setFriendId(userLike.getLikeUserId());
                        friend.setUserId(userLike.getUserId());
                        mongoTemplate.insert(friend);
                    }
                    flag = true;
                } catch (Exception e) {
                    flag = false;
                }
            }
        }
        mongoTemplate.save(userLike);
        return flag;
    }

    @Override
    public List<UserLike> findIsLike(Long userId,List<Long> visitorUserId) {
        Query query = Query.query(Criteria.where("userId").is(userId)
        .and("likeUserId").in(visitorUserId));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        return userLikes;
    }

    @Override
    public List<UserLike> like(Long userId, int page, int pageSize) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true))
                .skip((page-1)*pageSize).limit(pageSize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        return userLikes;
    }

    @Override
    public List<UserLike> fan(Long userId, int page, int pageSize) {
        Query query = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true))
                .skip((page-1)*pageSize).limit(pageSize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        return userLikes;
    }
}
